using AjeeleeCms.Web.Plugin.Link.BLL;
using AjeeleeCms.Web.Plugin.Link.Model;
using AjeeleeCms.Web.UI;
using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace AjeeleeCms.Web.Plugin.Link.admin
{
	public class link_edit : ManagePage
	{
		private string action = "Add";
		private int id = 0;
		protected HtmlForm form1;
		protected TextBox txtTitle;
		protected RadioButtonList rblIsLock;
		protected RadioButtonList rblIsRed;
		protected TextBox txtSortId;
		protected TextBox txtUserName;
		protected TextBox txtUserTel;
		protected TextBox txtEmail;
		protected TextBox txtSiteUrl;
		protected TextBox txtImgUrl;
		protected Button btnSubmit;
		protected void Page_Load(object sender, EventArgs e)
		{
			string text = base.Request.QueryString["action"];
			if (!string.IsNullOrEmpty(text) && text == "Edit")
			{
				this.action = "Edit";
				if (!int.TryParse(base.Request.QueryString["id"], out this.id))
				{
					base.JscriptMsg("传输参数不正确！", "back", "Error");
					return;
				}
				if (!new AjeeleeCms.Web.Plugin.Link.BLL.link().Exists(this.id))
				{
					base.JscriptMsg("信息不存在或已被删除！", "back", "Error");
					return;
				}
			}
			if (!this.Page.IsPostBack)
			{
				if (this.action == "Edit")
				{
					this.ShowInfo(this.id);
				}
			}
		}
		private void ShowInfo(int _id)
		{
			AjeeleeCms.Web.Plugin.Link.BLL.link link = new AjeeleeCms.Web.Plugin.Link.BLL.link();
			AjeeleeCms.Web.Plugin.Link.Model.link model = link.GetModel(_id);
			this.txtTitle.Text = model.title;
			ListControl arg_35_0 = this.rblIsRed;
			int num = model.is_red;
			arg_35_0.SelectedValue = num.ToString();
			ListControl arg_4F_0 = this.rblIsLock;
			num = model.is_lock;
			arg_4F_0.SelectedValue = num.ToString();
			TextBox arg_69_0 = this.txtSortId;
			num = model.sort_id;
			arg_69_0.Text = num.ToString();
			this.txtUserName.Text = model.user_name;
			this.txtUserTel.Text = model.user_tel;
			this.txtEmail.Text = model.email;
			this.txtSiteUrl.Text = model.site_url;
			this.txtImgUrl.Text = model.img_url;
		}
		private bool DoAdd()
		{
			bool result = true;
			AjeeleeCms.Web.Plugin.Link.Model.link link = new AjeeleeCms.Web.Plugin.Link.Model.link();
			AjeeleeCms.Web.Plugin.Link.BLL.link link2 = new AjeeleeCms.Web.Plugin.Link.BLL.link();
			link.title = this.txtTitle.Text.Trim();
			link.is_lock = int.Parse(this.rblIsLock.SelectedValue);
			link.is_red = int.Parse(this.rblIsRed.SelectedValue);
			link.sort_id = int.Parse(this.txtSortId.Text.Trim());
			link.user_name = this.txtUserName.Text.Trim();
			link.user_tel = this.txtUserTel.Text.Trim();
			link.email = this.txtEmail.Text.Trim();
			link.site_url = this.txtSiteUrl.Text.Trim();
			link.img_url = this.txtImgUrl.Text.Trim();
			link.is_image = 1;
			if (string.IsNullOrEmpty(link.img_url))
			{
				link.is_image = 0;
			}
			if (link2.Add(link) < 1)
			{
				result = false;
			}
			return result;
		}
		private bool DoEdit(int _id)
		{
			bool result = true;
			AjeeleeCms.Web.Plugin.Link.BLL.link link = new AjeeleeCms.Web.Plugin.Link.BLL.link();
			AjeeleeCms.Web.Plugin.Link.Model.link model = link.GetModel(_id);
			model.title = this.txtTitle.Text.Trim();
			model.is_lock = int.Parse(this.rblIsLock.SelectedValue);
			model.is_red = int.Parse(this.rblIsRed.SelectedValue);
			model.sort_id = int.Parse(this.txtSortId.Text.Trim());
			model.user_name = this.txtUserName.Text.Trim();
			model.user_tel = this.txtUserTel.Text.Trim();
			model.email = this.txtEmail.Text.Trim();
			model.site_url = this.txtSiteUrl.Text.Trim();
			model.img_url = this.txtImgUrl.Text.Trim();
			model.is_image = 1;
			if (string.IsNullOrEmpty(model.img_url))
			{
				model.is_image = 0;
			}
			if (!link.Update(model))
			{
				result = false;
			}
			return result;
		}
		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			if (this.action == "Edit")
			{
				if (!this.DoEdit(this.id))
				{
					base.JscriptMsg("保存过程中发生错误啦！", "", "Error");
				}
				else
				{
					base.JscriptMsg("修改成功啦！", "index.aspx", "Success");
				}
			}
			else
			{
				if (!this.DoAdd())
				{
					base.JscriptMsg("保存过程中发生错误啦！", "", "Error");
				}
				else
				{
					base.JscriptMsg("添加成功啦！", "index.aspx", "Success");
				}
			}
		}
	}
}
